<!--
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 14:41:20
 * @LastEditTime: 2023-10-03 13:48:55
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description:   :gridOps="gridOptions" inventory
-->
<template>
  <sc-VxeTable
    ref="xGrid"
    :gridEvts="gridEvents"
    :tablecode="'market_customer_complaint_finance'"
    :url="api.market.complaint.list"
  >
  </sc-VxeTable>

  <SaveDialog
    v-if="dialog.save"
    ref="saveDialog"
    @success="handleSuccess"
    @closed="dialog.save = false"
  ></SaveDialog>
  <skBrowserChoose
    v-model="dialog.storage"
    :name="'storage_browser'"
    @close="dialog.storage = false"
    @select="additems"
  >
  </skBrowserChoose>
  <skBrowserChoose
    v-model="dialog.retail"
    :name="'retail_browser'"
    @close="dialog.retail = false"
    @select="addTetail"
  >
  </skBrowserChoose>
</template>
<script>
export default {
  name: "market_customer_complaint_indexfinance",
};
</script>
<script setup>
import skBrowserChoose from "@/components/skBrowserChoose";
import XEUtils from "xe-utils";
// import { useRouter } from "vue-router";
import { ref, nextTick } from "vue";
// , onMounted, reactive, ref
// const router = useRouter();
import SaveDialog from "./save";
import api from "@/api";
const dialog = ref({
  save: false,
  storage: false,
  retail: false,
});

const xGrid = ref({});
const saveDialog = ref({});
const handleSuccess = () => {
  xGrid.value.refresh();
};
const addTetail = (data) => {
  dialog.value.save = true;
  let row = data[0];
  let rowdata = Object.assign({}, row.delivery, row);
  delete rowdata.id;
  rowdata.amount = XEUtils.toNumber(rowdata["quantity"] * row["price"]);
  rowdata["relation_code"] = row.code;
  rowdata["red_id"] = row.id;
  nextTick(() => {
    delete rowdata.delivery_id;
    delete rowdata.mpc_batch_id;
    delete rowdata.status_id;
    delete rowdata.code;
    saveDialog.value.setData(rowdata);
    saveDialog.value.open("edit");
  });
};
const additems = (data) => {
  dialog.value.storage = dialog.value.retail = false;
  dialog.value.save = true;
  nextTick(() => {
    let selectdata = data[0];
    selectdata["auxiliary_quantity"] = XEUtils.round(
      selectdata["available_auxiliary_quantity"] ?? selectdata["red_quantity"] / 25,
      2
    );
    selectdata["quantity"] = XEUtils.round(
      selectdata["available_quantity"] ?? selectdata["red_quantity"],
      2
    );
    selectdata["price"] = XEUtils.round(selectdata["price"], 2);
    selectdata["relation_code"] = selectdata.code ?? "";
    if (selectdata["relation_code"] != "") selectdata["red_id"] = selectdata.id;

    delete selectdata.complaint_id;
    delete selectdata.id;
    delete selectdata.code;
    delete selectdata.main_id;
    delete selectdata.status_id;
    delete selectdata.singler_id;
    //库存字段
    delete selectdata.in_quantity;
    delete selectdata.in_auxiliary_quantity;
    delete selectdata.occupy_quantity;
    delete selectdata.occupy_auxiliary_quantity;
    delete selectdata.out_quantity;
    delete selectdata.out_auxiliary_quantity;
    delete selectdata.available_quantity;
    delete selectdata.available_auxiliary_quantity;
    delete selectdata.remark;

    saveDialog.value.setData(selectdata);
    saveDialog.value.open("edit");
  });
};
const gridEvents = {
  cellDblclick: function ({ row }) {
    gridEvents.edit(row);
  },
  addStorage: function () {
    dialog.value.storage = true;
  },
  add: function () {
    dialog.value.retail = true;
  },

  edit: function (data) {
    dialog.value.save = true;
    if (typeof data.$grid !== "undefined") {
      data = data.$grid.getCheckboxRecords()[0];
    }
    nextTick(() => {
      saveDialog.value.setData(data);
      saveDialog.value.open("edit");
    });
  },
};
</script>
